/**
 * 进厂记录
 * @constructor
 */
function EntryRecord() {
}

EntryRecord.init = function () {

    $('#tableList').bootstrapTable({
        url: '/entryRecord/tableSearch',
        method: 'post',
        striped : true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle : true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
        //search : "true",
        uniqueId: "id",
        pageList : [ 5, 10, 20,50,100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search:false,
        showColumns: true,
        showRefresh: true,
        showExport: true,
        //得到查询的参数
        queryParams: function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
                //********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
                //********
                //获取查询的对象，往下加入
                sourceAddress: $('#search_SourceAddress').val(), // 请求时向服务端传递的参数
                timeInterval: $('#search_EntryDate').val(), // 请求时向服务端传递的参数
            };
            return query_params;
        },
        columns: [
            {
                //field: 'Number',//可不加
                title: '序号',//标题  可不加
                align: "center",
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize=$('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                    var pageNumber=$('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                    return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                }
            },
            { field: 'enterpriseName', title: '企业名称',align: 'center'},
            { field: 'entryDate', title: '进厂日期',align: 'center'},
            { field: 'sourceAddress', title: '来源地址',align: 'center'},
            { field: 'ownerName', title: '畜主名称',align: 'center'},
            { field: 'buyer', title: '购买人',align: 'center'},
            { field: 'type', title: '种类',align: 'center'},
            { field: 'number', title: '数量(头/只/羽)',align: 'center'},
            { field: 'quarantineNo', title: '检疫证号',align: 'center'},
            { field: 'livestock', title: '压栏数',align: 'center'},
            { field: 'userName', title: '上报人',align: 'center'},
            { field: 'reportDate', title: '上报日期',align: 'center'},
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    if(row.livestock<=0){
                        optionHtml = optionHtml+"<a class=\"btn btn-danger btn-xs\"><i class=\"fa fa-edit\"></i>该批次已全部屠宰</a>";
                    }else{
                        optionHtml = optionHtml+"<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"edit\")' title=\"屠宰\" ><i class=\"fa fa-edit\"></i>屠宰</a>";
                    }
                    return optionHtml;

                }
            }
        ]
    });
}
/**
 *时间选择器
 */
laydate.render({
    elem: '#text_entryDate',
    theme: 'molv',
    type:"date",
});
laydate.render({
    elem: '#search_EntryDate',
    range: true,
});
laydate.render({
    elem: '#text_SlaughterDate',
    theme: 'molv',
    type:"date",
});
//查询方法****************************
function doSearch() {
    $("#tableList").bootstrapTable("refreshOptions",{pageNumber:1});
}
//重置****************************
function doRest() {
    $("#search_SourceAddress").val("");
    $("#search_EntryDate").val("");
    $("#tableList").bootstrapTable("refreshOptions",{pageNumber:1});
}
//增加方法****************************
var Action="";
var Id=0;
var pxt="/entryRecord"; //控制器名称
function addRecord() {
    Id=0;
    Action="add";
    $("#myform")[0].reset();

}
//增加数据保存****************************
function saveRecord() {
    if(!validateForm()) return false;
    //创建传递的参数
    var postdata = {
        id: Id,
        action:Action,
        entryDate: $("#text_entryDate").val(),  //进厂日期
        sourceAddress: $("#text_sourceAddress").val(),  //来源地址
        ownerName: $("#text_ownerName").val(),  //畜主名称
        buyer: $("#text_buyer").val(),  //购买人
        type: $("#text_type").val(),    //类型
        number: $("#text_number").val(),    //数量(头/只/羽)
        // livestock: $("#text_number").val(),    //数量(头/只/羽)
        quarantineNo: $("#text_quarantineNo").val(),   //检疫证号
        // earMark: $("#text_grade").val(),  //耳标号
    };
    //发送异步请求到后台保存用户数据
    $.ajax({
        url:pxt+"/save",
        type:"post",
        data:JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.code === "200") {
                $('#myModalAdd').modal('hide')
                doSearch();
                swal("操作成功!", "请继续操作", "success");
            } else if(data.code === "501"){
                swal("操作失败!耳标号总数与数量不匹配!", "请重试", "error");
            }else{
                swal("操作失败!", "请重试", "error");
            }
        }
    });
}
var stype = "";
//屠宰数据保存****************************
function saveRecord2() {
    if(!validateForm2()) return false;
    //创建传递的参数
    var postdata = {
        entryRecordId: Id,
        slaughterDate: $("#text_SlaughterDate").val(),   //屠宰日期
        slaughterNumber: $("#text_SlaughterNumber").val(),   //屠宰数量
        type:stype,
    };
    //发送异步请求到后台保存用户数据
    $.ajax({
        url:"/slaughterRecord/save",
        type:"post",
        data:JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.code === "200") {
                $('#myModalAdd').modal('hide')
                $('#myModalAdd2').modal('hide')
                doSearch();
                swal("操作成功!", "请继续操作", "success");
            } else if (data.code === "501"){
                swal("压栏数不足!", "请重试", "error");
            }else{
                swal("操作失败!", "请重试", "error");
            }
        }
    });
}
//操作相关方法****************************
function actionRecord(data,type) {
        //修改
        Id=data.id;
        Action="edit";
        $("#myform2")[0].reset();
        $("#save2").removeAttr("disabled");
        stype = data.type;
        // WriteData(data)
        $('#myModalAdd2').modal('show')
}

//给控件赋值****************************
function WriteData(data) {
    //初始化耳标号
    $.ajax({
        url:'/entryRecord/tableSearchEarmark/'+data.id,
        type:"post",
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            $("#text_Earmark").empty();
            $.each(data.data,function (index,o) {
                $("#text_Earmark").append("<div class=\"col-lg-1\"><label><input type=\"checkbox\" value=\""+o.earMarkNo+"\"/>"+o.earMarkNo+"</label></div>")
            })
        }
    });
}

//表单验证*****************************
function validateForm() {
    //validate方法参数可选
    return $("#myform").validate({
        rules: {
            text_entryDate: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_sourceAddress: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_ownerName: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_buyer: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_type: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_number: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true, //必须是数字
            },
            text_quarantineNo: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_grade: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
        },
        //错误信息提示
        messages: {
            text_entryDate: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_sourceAddress: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_ownerName: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_buyer: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_type: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_number: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_quarantineNo: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_grade: {
                required: "(必填)",
                minlength: "最少1位",
            },
        },
    }).form();
}
//表单验证*****************************
    function validateForm2() {
        //validate方法参数可选
        return $("#myform2").validate({
            rules: {
                text_SlaughterDate: {
                    required: true,//必填
                    minlength: 1, //最少1个字符
                },
                text_SlaughterNumber: {
                    required: true,//必填
                    minlength: 1, //最少1个字符
                    number: true, //必须是数字
                },
            },
            //错误信息提示
            messages: {
                text_SlaughterDate: {
                    required: "(必填)",
                    minlength: "最少1位",
                },
                text_SlaughterNumber: {
                    required: "(必填)",
                    minlength: "最少1位",
                    number: "必须输入数字",
                },
            },
        }).form();
}